<?php

namespace App\Admin\Controllers;

use App\Models\User;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use EasyWeChat\Factory;
use Encore\Admin\Controllers\Dashboard;
use Encore\Admin\Widgets\Box;
use Encore\Admin\Widgets\InfoBox;
use App\Models\UserSummary;
use App\Models\UserCumulate;
use JavaScript;
use Log;

class UserController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('用户管理');
            $content->description('订阅的用户列表');
            $content->row($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(User::class, function (Grid $grid) {

            $grid->id('ID')->sortable();
            $grid->column('openid');
            $grid->column('nickname');
            $grid->column('city');
            $grid->column('subscribe_time')->display(function ($subscribe_time) {
                return date("Y-m-d H:i", $subscribe_time);
            });
            $grid->updated_at();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(User::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }

    protected function getUserFromMp($openId)
    {
        $config = [
            'app_id' => env('mp_appid'),
            'secret' => env('mp_appsecret'),
            'token' => env('mp_token'),
            'response_type' => 'array',

            'log' => [
                'level' => config('debug'),
                'file' => storage_path('logs/wechat.log'),
            ],
        ];

        $app = Factory::officialAccount($config);
        $user = $app->user->get($openId);
    }

    protected function getTotalCharts()
    {
        return Admin::content(function (Content $content) {

            $content->header('微信公众号数据统计');
            //$content->description('');

            //$content->row(view('admin::dashboard.test'));
            $fromday = date("Y-m-d", strtotime("-8 day"));
            $today = date("Y-m-d", strtotime("-1 day"));

            Log::info($fromday ."\t". $today);

            $userCumulates = UserCumulate::where("ref_date", ">=", $fromday)
                ->where("ref_date", "<=", $today)
                ->orderBy("ref_date", 'asc')
                ->limit(7)
                ->get();

            $labels = [];
            $data = [];
            foreach($userCumulates as $userCumulate) {
                $labels[] =  $userCumulate->ref_date;
                $data[] = $userCumulate->cumulate_user;
            }
            $box = new Box(
                '图文数据统计',
                view('admin.charts.bar')
                    ->with("labels", json_encode($labels))
                    ->with('data', json_encode($data))
            );
            $label = "公众号累计用户数";
            JavaScript::put([
                'labels' => $labels,
                'data' => $data,
                'msg' => "什么玩意",
                'label' => $label,
            ]);

            $content->row($box->render());

        });
    }
}
